package com.thread.exe;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class MonitorVehicleTracker {
	private final Map<String, MutablePoint> locations;

	public MonitorVehicleTracker(Map<String, MutablePoint> locations) {
		super();
		this.locations = deepCopy(locations);
	}
	
	public synchronized MutablePoint getLocations(String id){
		MutablePoint loc = locations.get(id);
		return loc == null ? null : new MutablePoint(loc);
	}
	
	public synchronized void setLocations(String id, int x, int y){
		MutablePoint loc = locations.get(id);
		if(loc == null)
			throw new IllegalArgumentException("No such ID: " + id);
		loc.x = x;
		loc.y = y;
	}

	private Map<String, MutablePoint> deepCopy(Map<String, MutablePoint> locations) {
		Map<String, MutablePoint> result = new HashMap<String, MutablePoint>();
		for (String id : locations.keySet()) {
			result.put(id, new MutablePoint(locations.get(id)));
		}
		return Collections.unmodifiableMap(result);
	}
}
